package face.pay.api.payter.channel;

import com.alibaba.fastjson.JSONObject;
import face.pay.api.payter.channel.paytype.alilpay.WxAliQrPay;
import face.pay.api.payter.channel.paytype.wechat.*;
import face.pay.constant.ChannelCode;
import face.pay.entity.TerminalInfo;
import face.pay.constant.PayClient;
import face.pay.util.ResultMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 微信渠道
 */
@Component
@Slf4j
public class WxPayChannel extends AbstractChannel {

    private WxFacePay wxFacePay;


    private WxQrPay wxQrPay;


    private WxJsPay wxJsPay;

    private WxAliQrPay aliQrPay;

    private FaceSharingPay wxFaceSharingPay;

    private WXQrSharingPay wxQrSharingPay;

    public WxPayChannel() {
    }

    @Autowired
    public WxPayChannel(WxFacePay wxFacePay, WxQrPay wxQrPay, WxJsPay wxJsPay, WxAliQrPay aliQrPay, FaceSharingPay wxFaceSharingPay, WXQrSharingPay wxQrSharingPay) {
        this.wxFacePay = wxFacePay;
        this.wxQrPay = wxQrPay;
        this.wxJsPay = wxJsPay;
        this.aliQrPay = aliQrPay;
        this.wxFaceSharingPay = wxFaceSharingPay;
        this.wxQrSharingPay = wxQrSharingPay;
    }

    @Override
    public String getChannelCode() {
        return ChannelCode.WECHAT;
    }

    @Override
    public ResultMessage facePay(JSONObject json) {

        /**
         * 微信设备上 刷脸只能走微信官方的刷脸接口
         */
        /**
         * 设备验证
         */
        String device_id = json.getString("device_id")==null?"":json.getString("device_id");
        String imei = json.getString("imei")==null?"":json.getString("imei");

        Long cardDetailId = json.getLong("cardDetailId");

        TerminalInfo terminalInfo  = this.terminalInfoValidate(device_id,imei);

        if(terminalInfo!=null){

            json.put("terminalId",terminalInfo.getId());
            json.put("channelCode",getChannelCode());

            if(cardDetailId!=null && cardDetailId>0){
                /**
                 * 微信优惠券核销
                 */
                return  wxFaceSharingPay.pay(json);
            }else {
                return wxFacePay.pay(json);
            }


        }else {
            return ResultMessage.createFail("设备信息验证有误，检查登录状态以及门店信息");
        }



    }

    @Override
    public ResultMessage qrPay(JSONObject json) {

        /**
         * 设备验证
         */
        String device_id = json.getString("device_id")==null?"":json.getString("device_id");
        String imei = json.getString("imei")==null?"":json.getString("imei");


        TerminalInfo terminalInfo  = this.terminalInfoValidate(device_id,imei);

        if(terminalInfo!=null){

            String auth_code = json.getString("auth_code");

            String barType = this.barType(auth_code);

            json.put("terminalId",terminalInfo.getId());
            json.put("channelCode",getChannelCode());
            Long cardDetailId =  json.getLong("cardDetailId");

            if(PayClient.ALIPAY.equals(barType)){
                return  aliQrPay.pay(json);
            }else if(PayClient.WECHAT.equals(barType)){

                if(cardDetailId!=null && cardDetailId>0){
                    /**
                     * 微信优惠券核销
                     */
                    return  wxQrSharingPay.pay(json);
                }else {
                    return wxQrPay.pay(json);
                }

            }else {
                return  ResultMessage.createFail("付款码识别错误");
            }

        }else {
            return ResultMessage.createFail("设备信息验证有误，检查登录状态以及门店信息");
        }

    }

    @Override
    public ResultMessage jsPay(JSONObject json) {

        /**
         * 设备验证
         */
        String device_id = json.getString("device_id")==null?"":json.getString("device_id");

        TerminalInfo terminalInfo  = this.getTermial(device_id);

        if(terminalInfo!=null && terminalInfo.getStoreId()!=null){

            json.put("terminalId",terminalInfo.getId());
            json.put("channelCode",getChannelCode());
            json.put("storeId",terminalInfo.getStoreId());

            return wxJsPay.pay(json);

           }else {
                return  ResultMessage.createFail("设备不存在或设备未绑定门店");
         }
    }
}
